package com.zhiyou100.controller;

import com.zhiyou100.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;

/**
 * action 响应数据的类型
 *
 * @author yang
 * @version 1.0.0
 * @date 2020-10-19 22:23
 */
@Controller
@RequestMapping("/test03")
public class Test03 {

    /**
     * a  响应一个字符串
     *
     * @return 字符串
     */
    @RequestMapping("/m1.action")
    @ResponseBody
    public String test01Method() {
        return "success";
    }

    /**
     * 响应一个页面的逻辑路径/真实路径
     *
     * @return templates/index.html 主页
     */
    @RequestMapping("/m2.action")
    public String test02Method() {
        return "index";
    }

    /**
     * 响应一个 ModelAndView
     *
     * @return 可以设置一个页面
     */
    @RequestMapping("/m3.action")
    public ModelAndView test03Method() {
        ModelAndView view = new ModelAndView();
        view.setViewName("/index.html");
        return view;
    }


    /**
     * ：d 响应一个json串
     *
     * @return json 串
     */
    @RequestMapping("/m4.action")
    @ResponseBody
    public User test04Method() {
        return User.builder()
                .age(10)
                .name("杨磊")
                .email("@fadsff")
                .build();
    }

    /**
     * 直接控制 HttpServletResponse
     *
     * @param resp HttpServletResponse
     * @throws Exception 异常
     */
    @RequestMapping("/m5.action")
    public void test05Method(HttpServletResponse resp) throws Exception {
        resp.getWriter().print("success");
    }
}
